﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class SysManager_BatchSetUserRights : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            sm.SelectedRows.Add(new SelectedRow(0));
            RowSelectionModel sm2 = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            sm2.SelectedRows.Add(new SelectedRow(0));
            BinUserList("");
            InsActLog(mid, "浏览");
        }
    }
    protected void Cell_Click(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        this.Store1.DataSource = mydb.DM_GetDateSet("Exc_UserModesRightSet " + sm.SelectedRecordID+",'"+this.txtmodulename.Text+"'");
        this.Store1.DataBind();
    }

    protected void BtnSetAllBrowse(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        RowSelectionModel user = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        string userid = user.SelectedRecordID;
        string mid_list="";
        string sql = "";
        foreach (SelectedRow row in sm.SelectedRows)
        {
            sql += @" if not exists(select * from sys_modulerights where mid=" + row.RecordID + " and userid=" + userid + @")
                        insert into sys_modulerights(userid,mid,creator,M_Browse)values("
                         + userid + "," + row.RecordID + ",'" + Session["UserID"] + @"',0) ";
            mid_list +=row.RecordID+",";
        }
        mydb.DM_ExecuteNonQuery(sql);
        
        SetModelRights(mid_list, userid, "M_Browse", "1");
    }
    protected void SetModelRights(string mid,string userid,string key,string val)
    {
        string sql = "update Sys_ModuleRights set " + key + "="+val+" where mid in(" + mid.TrimEnd(',') + ") and userid=" + userid;
        if (mydb.DM_ExecuteNonQuery(sql)!=-1)
        {
            ShowBox("修改成功！");
            Cell_Click(null, null);
        }
        else
        {
            ShowBox("修改失败！");
        }
    }
    protected void MyData_Refresh(object sender, StoreReadDataEventArgs e)
    {
        BinUserList(this.txtuser.Text);
    }
    protected void BtnQueryUser(object sender, DirectEventArgs e)
    {
        BinUserList(this.txtuser.Text);
    }
    public void BinUserList(string param)
    {
        string str = "";
        if (param != "")
        {
            str += " and usercode like '%" + param + "%' or username like '%" + param + "%'";
        }
        this.Store2.DataSource = mydb.DM_GetDateSet("select userid,usercode,username from sys_loginuser where 1=1" + str);
        this.Store2.DataBind();
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void Edit(int mid, int userid, string field, string oldValue, string newValue, object customer)
    {
        //string message = "<b>Property:</b> {0}<br /><b>Field:</b> {1}<br /><b>Old Value:</b> {2}<br /><b>New Value:</b> {3}";
        // Send Message...
        //X.Msg.Notify(new NotificationConfig()
        //{
        //    Title = "Edit Record #" + id.ToString(),
        //    Html = string.Format(message, id, field, oldValue, newValue),
        //    Width = 250
        //}).Show();
        //更新到数据库
        string sql = "";
        sql += @" if not exists(select * from sys_modulerights where mid=" + mid + " and userid=" + userid + @")
                        insert into sys_modulerights(userid,mid,creator,M_Browse)values("
                         + userid + "," + mid + ",'" + userid + @"',0) ";
        sql += " update Sys_ModuleRights set " + field + "=" + (newValue.ToLower() == "true" ? 1 : 0)
           + " where mid=" + mid + " and userid=" + userid;
        //X.Msg.Alert("Submit", "You have submitted " + sql + " nodes").Show();
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            this.GridPanel1.GetStore().GetById(mid).Commit();
        }
        else
        {
            X.Msg.Notify(new NotificationConfig()
            {
                Title = "消息提示",
                Html = "修改失败！",
                Width = 250
            }).Show();
        }
    }
}